// Aaron Schraufnagel
// TCSS 360 TeamDAMN
// For OnTrack System
// 11/25/14
package teamdamn.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import teamdamn.model.ResponseList;

/**
 * This panel is for the Search Functionality.
 * @author Aaron Schraufnagel
 * @date Last Edited: 11/25/14
 */
@SuppressWarnings("serial")
public class SearchPanel extends JPanel implements Observer {
	
	/**
	 * The text field that holds the search field.
	 */
	private JTextField searchTextField;

	/**
	 * A list of responses parsed from a text file.
	 */
	private ResponseList myResponses;
	
	/**
	 * The constructor of the search panel.
	 * @author Aaron Schraufnagel 
	 * @date Last Edited: 11/26/14
	 */
	public SearchPanel() {		
		buildSearchPanel();		
	}
	
	/**
	 * This method build the GUI of this panel
	 * @author Aaron Schraufnagel
	 * @date Last Edited: 11/25/14
	 */
	private void buildSearchPanel(){
		myResponses = new ResponseList();
		setBorder(new LineBorder(new Color(0, 0, 0), 4));
		
		JButton btnSearch = new JButton("Search");
		btnSearch.setFont(new Font("Tahoma", Font.BOLD, 11));
		
		searchTextField = new JTextField();
		searchTextField.setColumns(10);
		
		final JRadioButton rdbtnKeyword = new JRadioButton("Keyword");
		rdbtnKeyword.setSelected(true);
		
		JRadioButton rdbtnQuestion = new JRadioButton("Question");
		
		ButtonGroup search = new ButtonGroup();
		search.add(rdbtnKeyword);
		search.add(rdbtnQuestion);
		
		JLabel lblSearchResults = new JLabel("Search Results");
		lblSearchResults.setHorizontalAlignment(SwingConstants.CENTER);
		lblSearchResults.setForeground(Color.GREEN);
		lblSearchResults.setFont(new Font("Tahoma", Font.BOLD, 24));
		
		JButton btnBrowse = new JButton("Browse All");
		btnBrowse.setFont(new Font("Tahoma", Font.PLAIN, 11));
		
		final JButton btnClear = new JButton("Clear");
		btnClear.setEnabled(false);
		btnClear.setFont(new Font("Tahoma", Font.PLAIN, 11));
		
		JScrollPane scrollPane = new JScrollPane();
		
		final JButton btnCopy = new JButton("Copy");
		btnCopy.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnCopy.setEnabled(false);

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(12)
								.addComponent(rdbtnKeyword)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(rdbtnQuestion)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(btnBrowse, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(9)
								.addComponent(searchTextField, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnSearch)))
						.addComponent(lblSearchResults, GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
						.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
							.addComponent(btnClear, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnCopy, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnSearch)
						.addComponent(searchTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnBrowse)
						.addComponent(rdbtnQuestion)
						.addComponent(rdbtnKeyword))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblSearchResults)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnClear)
						.addComponent(btnCopy))
					.addContainerGap())
		);
		
		final JTextArea textResults = new JTextArea();
		textResults.setEditable(false);
		scrollPane.setViewportView(textResults);
		setLayout(groupLayout);
		
		searchTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (rdbtnKeyword.isSelected()) {
					String result = myResponses.getKeywordSearchResults(searchTextField.getText());
					if (result.equals("")) {
						textResults.setText("Your search returned no results.\nTry another search term.");
					} else {
						textResults.setText(result);
					}
				} else {
					String result = myResponses.getQuestionSearchResults(searchTextField.getText());
					if (result.equals("")) {
						textResults.setText("Your search returned no results.\nTry another search term.");
					} else {
						textResults.setText(result);
					}
				}						
				btnClear.setEnabled(true);
				btnCopy.setEnabled(true);
			}
		});

		// BUTTON ACTION
		
//		btnToRfp.addMouseListener(new MouseAdapter() {
//			@Override
//			public void mouseClicked(MouseEvent arg0) {				
//				//myGUI.searchToRFP();
//				myAnalystPanel.panelToRFP();
//			}
//		});
		
		// BUTTON ACTION
		
				btnSearch.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						if (rdbtnKeyword.isSelected()) {
							String result = myResponses.getKeywordSearchResults(searchTextField.getText());
							if (result.equals("")) {
								textResults.setText("Your search returned no results.\nTry another search term.");
							} else {
								textResults.setText(result);
							}
						} else {
							String result = myResponses.getQuestionSearchResults(searchTextField.getText());
							if (result.equals("")) {
								textResults.setText("Your search returned no results.\nTry another search term.");
							} else {
								textResults.setText(result);
							}
						}						
						btnClear.setEnabled(true);
						btnCopy.setEnabled(true);
					}
				});		
		
		// BUTTON ACTION
		
		btnClear.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				textResults.setText("");
				searchTextField.setText("");
				btnClear.setEnabled(false);
				btnCopy.setEnabled(false);
			}
		});		
		
		// BUTTON ACTION
		
		btnBrowse.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				textResults.setText(myResponses.toString());;
				btnClear.setEnabled(true);
				btnCopy.setEnabled(true);
			}
		});				
		
		// BUTTON ACTION
		
		btnCopy.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				//textResults.setText(textResults.getSelectedText());
				String copiedString = textResults.getSelectedText();
				StringSelection stringSelection = new StringSelection(copiedString);
				Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
				clpbrd.setContents(stringSelection, null);
			}
		});			
		
		
		
		
		
	}

	/* Updates the search panel with the new Response List.
	 * @author joelhun
	 */
	@Override
	public void update(Observable theResponseListObject, Object theUpdatedList) {
		if (theResponseListObject != null) {
			myResponses = (ResponseList) theResponseListObject;
		}
		
	}
}
